<?php

namespace App\Http\Controllers;

use App\Library\Lock;
use App\Library\Requests\Pay\PayAuctionBailRequest;
use App\Library\Requests\Pay\PayAuctionRequest;
use App\Library\Service\PayService;
use App\Library\YsdLib;
use Illuminate\Http\JsonResponse;
use Yansongda\Artful\Exception\ContainerException;
use Yansongda\Pay\Pay;

class PayController extends BaseController
{
    /**
     * __construct
     * @param PayService $service
     * @throws ContainerException
     */
    public function __construct(PayService $service)
    {
        Pay::config(YsdLib::config());
        $this->service = $service;
    }

    /**
     * 拉起支付
     * @param PayAuctionRequest $request
     * @return JsonResponse
     */
    public function auction(PayAuctionRequest $request): JsonResponse
    {
        return Lock::get($request->serialNumber . __CLASS__ . __FUNCTION__, function () use ($request) {
            return $this->service->auctionOrder($request);
        });
    }

    /**
     * 拉起支付
     * @param PayAuctionBailRequest $request
     * @return JsonResponse
     */
    public function auctionBail(PayAuctionBailRequest $request): JsonResponse
    {
        return Lock::get($request->serialNumber . __CLASS__ . __FUNCTION__, function () use ($request) {
            return $this->service->auctionBail($request);
        });
    }
}
